<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\cache\Driver\File;

class Post extends AdminController
{
   
    public function index()
    {
        $list = Db::name('post')->paginate(3);
        $roles = array();
        foreach ($list as $val)
        {
            // var_dump($val['user_id']);
            $user = Db::name('index_user')->where('user_id',$val['user_id'])->find();
            // var_dump($user['name']);
            $val['username'] = $user['username'];
            $roles[] = $val;

        }
        return view('post/index',[
            'list' => $roles,
            'page'=>$list
        ]);
    }

   

    public function details($id)
    {

        $list = Db::name('post')->where('post_id',$id)->find();
        $uesrname = Db::name('user')->where('id',$list['user_id'])->find();
        // var_dump($uesrname['username']);
        // die;
        $list['username'] = $uesrname['username'];
        // var_dump($list);
        // die;
        return view('post/details',[
            'list' => $list
        ]);
    }


    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit()
    {
        
        $data = Db::name('post')->find($_POST['postid']);
        
        // if ($data) {
        //     $data['status'] = true;
        // } else {
        //     $data['status'] = false;
        // }
        // // $info['status'] = true;
        return json($data);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update()
    {
        
        $data = [
            'postname' => $_POST['postname'],
            'posthot' => $_POST['status']

        ];
        $result = Db::name('post')->where('post_id',$_POST['idid'])->update($data);
        $info['status'] = true;
        return json($info);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {


        $data = Db::name('post')->find($_POST['postid']);//获取当前帖子的信息

        //根据帖子id查出评论
        $pcom = Db::name('postcomment')->where('post_id',$_POST['postid'])->select();

        //判断是否有评论
        if(!empty($pcom)){
            //有的话进行删除
            foreach($pcom as $val){
                
                //根据评论的id查询追评数据
                $prdata =  Db::name('prcomment')->where('com_id',$val['postcom_id'])->select();
                // var_dump($val);
                // die;
                //判断追评是否为空
                if(!empty($prdata)){
                    //为空的话遍历删除追评
                    foreach($prdata as $v){
                        // var_dump($v);
                        // die;
                        $result = Db::name('prcomment')->where('prc_id',$v['prc_id'])->delete(); 
                    }

                }

                $result = Db::name('postcomment')->where('postcom_id',$val['postcom_id'])->delete();//删除评论
  
            }
            
        }
        
        if(!empty($data['artpic'])){//判断帖子是否有图片来进行删图
            $url = './static/postpic/'.$data['artpic'];
            unlink($url);
        }

        $result = Db::name('post')->delete($_POST['postid']);//删除文章呢

        if ($result > 0) {
            $info['status'] = true;
        } else {
            $info['status'] = false;
        }
        // $info['status'] = true;
        return json($info);

        
    }
}
